#include <bits/stdc++.h>

#define eb emplace_back
#define ep emplace
#define fi first
#define se second
#define in read<int>()
#define lin read<ll>()
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;

template < typename T > T read() {
	T x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

template < typename T > void chkmax(T &x, const T &y) { x = x > y ? x : y; }
template < typename T > void chkmin(T &x, const T &y) { x = x < y ? x : y; }

const int N = 1e5 + 10;

vec G[N];
int n, m, fa[N], f[N], pot[N], dep[N], tfa[N];
ll ans;

int gf(int x) { return fa[x] == x ? x :  fa[x] = gf(fa[x]); }

void dfs(int x, int p) {
	dep[x] = dep[p] + 1; tfa[x] = p; f[x] = dep[x] & 1 ? 1 : -1;
	for(auto y : G[x])
		if(y ^ p) dfs(y, x), f[x] += f[y];
	cerr << x << " " << f[x] << endl;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin);
#endif
	n = in, m = in; int rt, tr;
	rep(i, 1, m ) {
		int u = in, v = in;
		if(gf(u) == gf(v)) rt = u, tr = v, cerr << rt << endl;
		else G[u].eb(v), G[v].eb(u), fa[gf(u)] = gf(v);
	}
	dfs(rt, 0);
	if(n - 1 == m) {
		if(f[rt]) ans = -1;
	} else {
		int top, x = tr;
		for(; x; x = tfa[x]) pot[++top] = f[x];
		if(dep[tr] & 1) { // even
			if(f[rt]) ans = -1;
			sort(pot + 1, pot + top + 1);
			x = tr;
			for(; x; x = tfa[x]) f[x] -= pot[top >> 1];
		} else { // odd
			if(f[rt] & 1) ans = -1;
			x = tr; for(; x; x = tfa[x]) f[x] -= f[rt] >> 1;
		}
	} if(ans == -1) return puts("-1"), 0;
	rep(i, 1, n) ans += abs(f[i]); cout << ans << endl;
	return 0;
}
